<?php
//测试控制器类
class IndexController extends Controller{
    //动作方法
    private $category;
    public function __init(){
    	$this->category=S('category');
    }

    public function index(){
        //获取按层级关系显示的栏目数据
        $category=Data::channelLevel($this->category);
        // p($category);exit;
       	$this->assign('category',$category);
        //读取所有商品
        $gid=Q('gid');
        $goods=M('goods')->all();
        $this->assign('goods',$goods);
        //详情页图片分配
        $map['goods_gid']=array('IN',$gid);
        $gallery=M('goods_gallery')->where($map)->all();
        // p($gallery);
        $this->assign('gallery',$gallery);
        //分配商品附表信息
        $goods_data=M('goods_data')->where($map)->all();
        $this->assign('goods_data',$goods_data);
        $this->display();
    }

    public function category(){
    	//获取栏目属性
    	$attr=$this->getAttr();
    	// p($attr);
    	$this->assign('attr',$attr);
    	$this->assignCategoryGoods();
    	$this->display();

    }

    //详情页商品
    public function goods(){
        $gid=Q('gid',0,'intval');
        $db=M('goods');
        $field=$db->find($gid);
        // p($field);
        $this->assign('field',$field);
        $goodsAttr=$this->getGoodsAttr($gid);
        // p($goodsAttr);
        $this->assign('goodsAttr',$goodsAttr);
        //详情页图片分配
        $map['goods_gid']=array('IN',$gid);
        $gallery=M('goods_gallery')->where($map)->all();
        // p($gallery);
        $this->assign('gallery',$gallery);
        //分配商品附表信息
        $goods_data=M('goods_data')->where($map)->all();
        $this->assign('goods_data',$goods_data);
        $this->display();
    }

    //获取商品属性
    public function getGoodsAttr($gid){
        $db=M('goods');
        $sql="SELECT * FROM attribute a JOIN goods_attr ga ON a.attr_id=ga.attr_id
            WHERE ga.goods_gid=$gid GROUP BY a.attr_id ORDER BY ga.attr_id ASC";
        $attr=$db->query($sql);
        //获取属性值
        foreach ($attr as $id => $v) {
            $sql="SELECT * FROM goods_attr ga WHERE attr_id={$v['attr_id']} AND goods_gid=$gid";
            $attr[$id]['attr']=$db->query($sql);
        }
        return $attr;
    }

    //获取货品信息
    public function getProduct(){
        //异步post传过来的id
        $id=Q('id',0,'trim');
        //货品表中的商品属性字段(商品属性表中的id字段拼接)
        $map['goods_attr']=$id;
        $data=M('product')->where($map)->find();
        if($data){
            $return=array('status'=>'success','data'=>$data);
        }else{
            $return=array('status'=>'error','data'=>$data);
        }
        echo json_encode($return);
        exit;
    }

    //获取商品
    private function assignCategoryGoods(){
    	//属性处理，去掉值为0的属性
    	$s=Q('get.s',0,'trim');
    	$s=explode('-',$s);
    	$s=array_filter($s);
    	//栏目ID
    	$cid=Q('cid',0,'intval');
    	//没有筛选属性时，获取所有商品
    	if(empty($s)){
    		$db= M('goods');
            $map['category_cid']=array('IN',$cid);
            $page= new Page($db->where($map)->count(),10);
            $data =$db->where($map)->limit($page->limit())->all();
            // p($data);
    	}else{
    		//属性ID对应的值
    		$map['id']=array('IN',$s);
    		$attr_value=M('goods_attr')->where($map)->getField('attr_value',true);
    		// p($attr_value);
    		$attr_value="'".implode("','",$attr_value)."'";
    		// p($attr_value);exit;
    		$sql="select count(*) c from goods_attr ga join goods g on ga.goods_gid=g.gid where ga.attr_value IN ({$attr_value}) group by ga.goods_gid having count(*)=".count($s);
            /*
            $sql="select count(*) as c from (select * from goods_attr ga join goods g on ga.goods_gid=g.gid where ga.attr_value IN ({$attr_value}) "
            */    		
    		// p($data);
    		//分页处理
    		$count =M()->query($sql);
            $count = empty($count)?0:$count;
    		// p($count);exit;
    		$page=new Page($count[0]['c'],10);
    		$sql="select * from goods_attr ga join goods g on ga.goods_gid=g.gid where ga.attr_value IN ({$attr_value}) group by ga.goods_gid having count(*)=".count($s)." limit ".$page->limit();
    		$data=M()->query($sql);
    	}

    	$this->assign('data',$data);
    	$this->assign('page',$page->show());
    }

    //获取栏目属性
    private function getAttr(){
    	//变量s是属性筛选的变量，没有s时产生默认的s,他的值是属性的连接
    	//如：颜色-尺寸-款式 都没有选择时显示（0-0-0）
    	$s=Q('get.s',null,'trim');  //null也可以
    	$cid=Q('cid',0,'intval');
    	//栏目中增加一个记录商品类型的字段cat_id;
    	// p($this->category);exit;
    	$cat_id=$this->category[$cid]['cat_id'];
    	// p($cat_id);
    	if(!$s){
    		//没有s变量时，读取栏目所属类型的所有属性值
    		$sql=" SELECT * FROM attribute a JOIN goods_attr ga ON a.attr_id=ga.attr_id WHERE a.cat_id={$cat_id} GROUP BY  a.attr_id";
    		$db=M();
    		$count=count($db->query($sql));
    		$s='';
    		for($i=0;$i<$count;$i++){
    			$s.='0-';
    		}
    		$s=substr($s,0,-1);
    		go(U('category',array('cid'=>$cid,'s'=>$s)));   		

    	}else{
    		//有s值时，获得当前栏目的所有属性用于搜索
    		$sql=" SELECT * FROM attribute a JOIN goods_attr ga ON a.attr_id=ga.attr_id WHERE a.cat_id={$cat_id} GROUP BY a.attr_id";
    		$db=M();
    		$attr=$db->query($sql);
    		foreach ($attr as $id => $a) {
    			$sql="select * from goods_attr ga where ga.attr_id={$a['attr_id']} group by attr_value";
    			// p($sql);exit;
    			$attr[$id]['_value']=$db->query($sql);
    		}
    		return $attr;
    	}
    	

    }



}
